<template>
  <div class="map-page">
	<div class="map-text">车牌号：{{route.query.carNo}}</div>
	<div class="map-text">车辆类型：{{route.query.typeName}}</div>
    <div ref="mapContainer" class="map-container"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
// 导入本地图片 - 确保路径正确
import markerIcon from '@/assets/images/car.png'

const route = useRoute()
const mapContainer = ref(null)
const map = ref(null)
const marker = ref(null)
const currentLng = ref(113.404)
const currentLat = ref(22.915)
const address = ref('')

// 从URL获取经纬度参数
const getCoordsFromURL = () => {
  const lng = parseFloat(route.query.lng)
  const lat = parseFloat(route.query.lat)
  
  // 验证坐标有效性
  if (!isNaN(lng) && !isNaN(lat)) {
    currentLng.value = lng
    currentLat.value = lat
  } else {
    console.warn('URL中没有有效的经纬度参数，使用默认坐标')
  }
}

// 初始化地图
const initMap = () => {
  getCoordsFromURL()
  
  // 创建地图实例
  map.value = new BMap.Map(mapContainer.value)
  const point = new BMap.Point(currentLng.value, currentLat.value)
  map.value.centerAndZoom(point, 18) // 18级缩放
  
  // 添加地图控件
  map.value.addControl(new BMap.NavigationControl())
  map.value.addControl(new BMap.ScaleControl())
  map.value.enableScrollWheelZoom()
  
  // 添加标记
  addMarker(point)
  
  // 获取地址信息
  getAddress(point)
}

// 添加标记
const addMarker = (point) => {
  if (marker.value) {
    map.value.removeOverlay(marker.value)
  }
  
  // 使用本地图片创建自定义图标
  const icon = new BMap.Icon(
      markerIcon,
      new BMap.Size(32, 32), // 这里设置你图片的实际显示尺寸
      {
        offset: new BMap.Size(16, 32), // 根据实际需要调整
        imageOffset: new BMap.Size(0, 0),
        imageSize: new BMap.Size(32, 32) // 新增：指定图片原始尺寸
      }
    )
  
  // 创建标记
  marker.value = new BMap.Marker(point, { icon })
  map.value.addOverlay(marker.value)
}

// 获取地址信息
const getAddress = (point) => {
  const geoc = new BMap.Geocoder()
  geoc.getLocation(point, (rs) => {
    const addComp = rs.addressComponents
    address.value = `${addComp.province} ${addComp.city} ${addComp.district} ${addComp.street} ${addComp.streetNumber}`
  })
}

// 监听URL参数变化
watch(() => route.query, (newQuery) => {
  if (newQuery.lng && newQuery.lat) {
    const lng = parseFloat(newQuery.lng)
    const lat = parseFloat(newQuery.lat)
    
    if (!isNaN(lng) && !isNaN(lat)) {
      currentLng.value = lng
      currentLat.value = lat
      
      if (map.value) {
        const newPoint = new BMap.Point(lng, lat)
        map.value.setCenter(newPoint)
        addMarker(newPoint)
        getAddress(newPoint)
      }
    }
  }
})

onMounted(() => {
  // 确保百度地图API已加载
  if (typeof BMap !== 'undefined') {
    initMap()
  } else {
    console.error('百度地图API未正确加载')
  }
})
</script>

<style scoped>
.map-text{
	margin-bottom:16px;
}
.map-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding:16px;
  background-color: #fff;
  font-size:18px;
}

.map-container {
  flex: 1;
  width: 100%;
  min-height: 400px;
  background-color: #fff;
}

.coord-info {
  padding: 12px;
  background: white;
  border-top: 1px solid #eee;
  font-family: monospace;
  line-height: 1.6;
}
</style>